﻿using MediatR;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Domains.zjy;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;
using System.Security;

namespace RearLivestock.RBAC.Api.Application.zjyApplication.Commands.Permissionses
{
    public class SearchRolePermissionsHandler : IRequestHandler<SearchRolePermissionsCommand, Result<PageResultDto<RolePermissionsDto>>>
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchRolePermissionsHandler> _logger;

        /// <summary>
        /// 权限仓储
        /// </summary>
        private readonly IBaseRepository<Permissions> _permissionsRepository;

        /// <summary>
        /// 角色权限仓储
        /// </summary>
        private readonly IBaseRepository<RolePermissions> _rolePermissionsRepository;

        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IBaseRepository<Roles> _rolesRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="permissionsRepository"></param>
        /// <param name="rolePermissionsRepository"></param>
        /// <param name="rolesRepository"></param>
        public SearchRolePermissionsHandler(ILogger<SearchRolePermissionsHandler> logger, IBaseRepository<Permissions> permissionsRepository, IBaseRepository<RolePermissions> rolePermissionsRepository, IBaseRepository<Roles> rolesRepository)
        {
            _logger = logger;
            _permissionsRepository = permissionsRepository;
            _rolePermissionsRepository = rolePermissionsRepository;
            _rolesRepository = rolesRepository;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<PageResultDto<RolePermissionsDto>>> Handle(SearchRolePermissionsCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<RolePermissionsDto>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                //分页
                var page = new PageResultDto<RolePermissionsDto>() { TotalCount = 0, PageCount = 0, List = new List<RolePermissionsDto>() };
                var query = (from a in await _rolesRepository.GetAll()
                             join b in await _rolePermissionsRepository.GetAll()
                             on a.RolesId equals b.RolesId
                             join c in await _permissionsRepository.GetAll()
                             on b.PermissionsId equals c.PermissionsId
                             where b.IsDeleted == false
                             select new RolePermissionsDto()
                             {
                                 RolesId = a.RolesId,
                                 RolesName = a.RolesName,
                                 RolePermissionsId = b.RolePermissionsId,
                                 Operate = b.Operate,
                                 PermissionsId = c.PermissionsId,
                                 PermissionsFunction = c.PermissionsFunction,
                                 IsDeleted = b.IsDeleted,
                             }).ToList();
                //总条数
                page.TotalCount = query.Count();
                //总页数
                page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
                //跳过几条 显示几条
                page.List = query.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                if (query != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "权限查询成功";
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("获取权限功能异常" + ex.Message);
            }
            return result;
        }
    }
}
